﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ww.wwf.wwfbll;
using ww.form.lis.com;
using System.Collections;
using ww.lis.lisbll.sam;
using ww.form.Properties;
using System.Xml;
using System.Drawing.Printing;

namespace ww.form.lis.sam.jy
{

    public partial class JYQueryForm : com.JyBaseForm
    {
        #region 自定义变量
        jybll blljy = new jybll();

        DataTable dtIMG = new DataTable();
        
        /// <summary>
        /// 当前行 样本
        /// </summary>
        DataRowView rowCurrent = null;
        /// <summary>
        /// 样本ID
        /// </summary>
        //string strfsample_id = "";

        /// <summary>
        /// 检验结果ID
        /// </summary>
        string strfjy_id = "";

        /// <summary>
        /// 申请ID
        /// </summary>
        //string strfapply_id = "";
        /// <summary>
        /// 审核标记
        /// </summary>
        string strfexamine_flag = "0";

        /// <summary>
        /// 仪器结果ID
        /// </summary>
        string strfinstr_result_id = "";

        /// <summary>
        /// 仪器列表
        /// </summary>
        DataTable dtInstr = null;
        #endregion

        #region 自定义方法
        /// <summary>
        /// 本页初始化方法
        /// </summary>
        private void WWInit()
        {
            try
            {
                this.com_listBindingSource_fstate.DataSource = this.WWComTypeSampleState();//检验状态
                this.com_listBindingSource_fjytype_id.DataSource = this.WWCheckType();//检验类别
                this.com_listBindingSource_fsample_type_id.DataSource = this.WWSampleType();//样本类别
                this.com_listBindingSource_fapply_dept_id.DataSource = this.WWApplyDept();//申请部门

                this.cmb_dept.DataSource = com_listBindingSource_fapply_dept_id;
                this.cmb_dept.DisplayMember = "fname";
                this.cmb_dept.ValueMember = "fdept_id";
                this.cmb_dept.SelectedIndex = -1;

                this.com_listBindingSource_fapply_user_id.DataSource = this.WWUserApply();//申请人
                this.com_listBindingSource_fjy_user_id.DataSource = this.WWUserCheck();//检验人
                dtInstr = this.bllInstr.BllInstrDTByUseAndGroupID(1, "kfz");//仪器列表 ww.wwf.wwfbll.LoginBLL.strDeptID 查询所有仪器
                this.fjy_instrComboBox.DataSource = dtInstr;
                fjy_instrComboBox.SelectedValue = "";
                this.ftype_idComboBox.DataSource = this.WWComTypeftype_id();//病人类别 
                ftype_idComboBox.SelectedValue = "";

                dataGridViewReport.AutoGenerateColumns = false;
                dataGridViewResult.AutoGenerateColumns = false;

                DataColumn D0 = new DataColumn("FID", typeof(System.String));
                dtIMG.Columns.Add(D0);

                DataColumn D1 = new DataColumn("结果图1", typeof(System.Byte[]));
                dtIMG.Columns.Add(D1);

                DataColumn D2 = new DataColumn("结果图2", typeof(System.Byte[]));
                dtIMG.Columns.Add(D2);

                DataColumn D3 = new DataColumn("结果图3", typeof(System.Byte[]));
                dtIMG.Columns.Add(D3);

                DataColumn D4 = new DataColumn("结果图4", typeof(System.Byte[]));
                dtIMG.Columns.Add(D4);

                DataColumn D5 = new DataColumn("结果图5", typeof(System.Byte[]));
                dtIMG.Columns.Add(D5);
            }
            catch (Exception ex)
            {
                WWMessage.MessageShowError(ex.ToString());
            }
        }
        /// <summary>
        /// 结果列显示 
        /// </summary>
        private void bllResultShow(string str仪器id)
        {
            if (str仪器id.Equals("mby_lb_mk3"))
            {
                this.fod.Visible = true;
                this.fcutoff.Visible = true;
            }
            else
            {
                this.fod.Visible = false;
                this.fcutoff.Visible = false;
            }
        }


        /// <summary>
        /// 取得报告列表
        /// </summary>
        private void WWSetReportList()
        {
            if (this.dtReportResult != null)
                this.dtReportResult.Clear();
           
            try
            {
                Cursor = Cursors.WaitCursor;
                string fapply_id = "";//条码号
                string fhz_zyh = "";//患者ID
                string fjy_zt = "";//检验状态
                string fhz_dept = "";//病人部门
                string fjy_yb_code = "";//样本号
                string fname = "";//姓名                
                string finstr_id = "";//仪器ID
                string fhz_type_id = "";//患者类别
                string fjy_date1 = this.bllReport.DbDateTime1(this.fjy_dateDateTimePicker1);
                string fjy_date2 = this.bllReport.DbDateTime1(this.fjy_dateDateTimePicker2);
                string fjy_dateWhere = "";
                fjy_yb_code = this.fsample_codeTextBox.Text;
                fname = this.fnameTextBox.Text;
                if (object.Equals(cmb_dept.SelectedValue, null))//病人部门
                    fhz_dept = "";
                else
                    fhz_dept = cmb_dept.SelectedValue.ToString();
                fapply_id = this.fapply_idtextBox.Text;
                fhz_zyh = this.fhz_zyhtextBox.Text;
                fjy_zt = this.fjy_ztComboBox.Text;

                try
                {
                    finstr_id = fjy_instrComboBox.SelectedValue.ToString();
                }
                catch { }
                try
                {
                    fhz_type_id = ftype_idComboBox.Text.Trim();
                }
                catch { }
                
                if (fjy_instrComboBox.Text == "" || fjy_instrComboBox.Text == null)
                    finstr_id = " (fjy_instr is not null) ";
                else
                    finstr_id = " (fjy_instr='" + finstr_id + "') ";

                if (this.ftype_idComboBox.Text == "" || this.ftype_idComboBox.Text == null)
                    fhz_type_id = " and (fhz_type_id is not null)";
                else
                    fhz_type_id = " and (fhz_type_id='" + fhz_type_id + "') ";

                fjy_dateWhere = " and (fjy_date>='" + fjy_date1 + "' and fjy_date<='" + fjy_date2 + "') ";

                if (fjy_yb_code == "" || fjy_yb_code == null)
                    fjy_yb_code = " and (fjy_yb_code is not null) ";
                else
                    fjy_yb_code = " and (fjy_yb_code='" + fjy_yb_code + "')";

                if (fname == "" || fname == null)
                    //fname = " and (fhz_name is not null) ";
                    fname = "";
                else
                    fname = " and (fhz_name like '" + fname + "%')";

                if (fhz_dept == "" || fhz_dept == null)
                    //fhz_dept = " and (fhz_dept is not null) ";
                    fhz_dept="";
                else
                    fhz_dept = " and (fhz_dept='" + fhz_dept + "')";

                if (fhz_zyh == "" || fhz_zyh == null)
                    //fhz_id = " and (fhz_id is not null) ";
                    fhz_zyh = "";
                else
                    fhz_zyh = " and (fhz_zyh='" + fhz_zyh + "')";

                if (fapply_id == "" || fapply_id == null)
                    //fhz_id = " and (fhz_id is not null) ";
                    fapply_id = "";
                else
                    fapply_id = " and (fapply_id='" + fapply_id + "')";

                if (fjy_zt == "" || fjy_zt == null)
                    fjy_zt = " and (fjy_zt is not null) ";
                else
                    fjy_zt = " and (fjy_zt='" + fjy_zt + "')";

                string strWhere = finstr_id + fhz_type_id + fjy_dateWhere + fjy_yb_code + fname + fhz_dept + fhz_zyh + fapply_id + fjy_zt;

                this.WWSetdtReportView(strWhere);
                this.lIS_REPORTBindingSource.DataSource = this.dtReport;
                dataGridViewReport.DataSource = this.lIS_REPORTBindingSource;
                
               
            }
            catch (Exception ex)
            {
                WWMessage.MessageShowError(ex.ToString());
            }
            finally
            {
                Cursor = Cursors.Arrow;
            }
        }
        
        private void WWResultList()
        {
            try
            {
                this.WWSetdtReportResultView(this.strfjy_id);
                this.bindingSourceResult.DataSource = this.dtReportResult;
                this.dataGridViewResult.DataSource = this.bindingSourceResult;

                #region 根据标识显示颜色
                if (this.dataGridViewResult.Rows.Count > 0)
                {
                    for (int intGrid = 0; intGrid < this.dataGridViewResult.Rows.Count; intGrid++)
                    {
                        string strBJ = this.dataGridViewResult.Rows[intGrid].Cells["fitem_badge"].Value.ToString();
                        string strValue = this.dataGridViewResult.Rows[intGrid].Cells["fvalue"].Value.ToString();

                        if (strValue.Contains("+") || strValue.Contains("阳"))
                        {
                            this.dataGridViewResult.Rows[intGrid].Cells["fitem_name"].Style.ForeColor = Color.Green;
                            this.dataGridViewResult.Rows[intGrid].Cells["fvalue"].Style.ForeColor = Color.Green;
                            this.dataGridViewResult.Rows[intGrid].Cells["fitem_badge"].Style.ForeColor = Color.Green;
                            continue;
                        }

                        if (strBJ.Equals("↑"))
                        {
                            this.dataGridViewResult.Rows[intGrid].Cells["fitem_name"].Style.ForeColor = Color.Red;
                            this.dataGridViewResult.Rows[intGrid].Cells["fvalue"].Style.ForeColor = Color.Red;
                            this.dataGridViewResult.Rows[intGrid].Cells["fitem_badge"].Style.ForeColor = Color.Red;
                        }
                        else if (strBJ.Equals("↓"))
                        {
                            this.dataGridViewResult.Rows[intGrid].Cells["fitem_name"].Style.ForeColor = Color.Blue;
                            this.dataGridViewResult.Rows[intGrid].Cells["fvalue"].Style.ForeColor = Color.Blue;
                            this.dataGridViewResult.Rows[intGrid].Cells["fitem_badge"].Style.ForeColor = Color.Blue;
                        }
                    }
                }
                #endregion
               
            }
            catch (Exception ex)
            {
                WWMessage.MessageShowError(ex.ToString());
            }
        }
     
        /// <summary>
        /// 快捷键 设置
        /// </summary>
        /// <param name="msg"></param>
        /// <param name="keyData"></param>
        /// <returns></returns>
        protected override bool ProcessCmdKey(ref   System.Windows.Forms.Message msg, System.Windows.Forms.Keys keyData)
        {
            if (keyData == (Keys.F1))
            {
                toolStripButtonHelp.PerformClick();
                return true;
            }
            if (keyData == (Keys.F5))
            {
                toolStripButtonQuery.PerformClick();
                return true;
            } if (keyData == (Keys.F8))
            {
                toolStripButtonPrint.PerformClick();
                return true;
            } if (keyData == (Keys.F9))
            {
                toolStripButtonPrintYL.PerformClick();
                return true;
            } 
            return base.ProcessCmdKey(ref   msg, keyData);
        }

        /// <summary>
        /// 打印
        /// </summary>
        /// <param name="printType"></param>
        private void WWPrint(int printType)
        {
            try
            {
                ww.form.lis.sam.Report.JyPrint pr = new ww.form.lis.sam.Report.JyPrint();
                pr.BllPrintViewer(printType, this.strfjy_id);
            }
            catch (Exception ex)
            {
                WWMessage.MessageShowError(ex.ToString());
            }
        }
        DataTable dtIMG_1 = new DataTable();
        private void WWImg()
        {
            try
            {
                if (this.dtIMG.Rows.Count > 0)
                    this.dtIMG.Clear();
                if (this.dtIMG_1.Rows.Count > 0)
                    this.dtIMG_1.Clear();
                dtIMG_1 = this.bllReport.BllImgDT(this.strfjy_id);
                DataRow drimg = dtIMG.NewRow();
                int intImgCount = dtIMG_1.Rows.Count;
                if (intImgCount > 0)
                {
                    groupBoxImg.Visible = true;

                    drimg["FID"] = dtIMG_1.Rows[0]["fimg_id"];
                    for (int iimg = 0; iimg < intImgCount; iimg++)
                    {
                        switch (iimg)
                        {
                            case 0:
                                img1.Visible = true;
                                img2.Visible = false;
                                img3.Visible = false;
                                img4.Visible = false;
                                img5.Visible = false;
                                drimg["结果图1"] = dtIMG_1.Rows[iimg]["fimg"];
                                break;
                            case 1:
                                img2.Visible = true;
                                img3.Visible = false;
                                img4.Visible = false;
                                img5.Visible = false;
                                drimg["结果图2"] = dtIMG_1.Rows[iimg]["fimg"];
                                break;
                            case 2:
                                img3.Visible = true;
                                img4.Visible = false;
                                img5.Visible = false;
                                drimg["结果图3"] = dtIMG_1.Rows[iimg]["fimg"];
                                break;
                            case 3:
                                img4.Visible = true;
                                img5.Visible = false;
                                drimg["结果图4"] = dtIMG_1.Rows[iimg]["fimg"];
                                break;
                            case 4:
                                img5.Visible = true;
                                drimg["结果图5"] = dtIMG_1.Rows[iimg]["fimg"];
                                break;
                            default:
                                break;
                        }
                    }

                }
                else
                {
                    groupBoxImg.Visible = false;
                    img1.Visible = false;
                    img2.Visible = false;
                    img3.Visible = false;
                    img4.Visible = false;
                    img5.Visible = false;
                }
                dtIMG.Rows.Add(drimg);
                lIS_REPORT_IMGBindingSource.DataSource = this.dtIMG;
            }
            catch (Exception ex)
            {
                WWMessage.MessageShowError(ex.ToString());
            }
        }
        #endregion

        public JYQueryForm()
        {
            InitializeComponent();
            WWInit();
        }

        private void ReportQueryForm_Load(object sender, EventArgs e)
        {
            if (blljy.str_WWF_SYS("brcxPrint") == "1" && LoginBLL.strPersonID == "brcx")//病人查询是否允许打印  0默认允许  1不允许
            {
                toolStripButtonPrint.Visible = false;
                toolStripButtonPrintYL.Visible = false;

                fjy_ztComboBox.Enabled = false;
                string[] str_UserCustom = Settings.Default.UserCustom.Split('|');
                if (!string.IsNullOrEmpty(str_UserCustom[1]))
                {
                    cmb_dept.SelectedIndex = cmb_dept.FindString(str_UserCustom[1]);
                    cmb_dept.Enabled = false;
                }
            }


            if (!string.IsNullOrEmpty(Program.str_args))
            {
                fhz_zyhtextBox.Text = Program.str_args;
            }
            try
            {
                WWSetReportList();
                
            }

            catch (Exception ex)
            {
                WWMessage.MessageShowError(ex.ToString());
            }
        }



        private void lIS_REPORTBindingSource_PositionChanged(object sender, EventArgs e)
        {
            try
            {
                rowCurrent = (DataRowView)this.lIS_REPORTBindingSource.Current;
                if (this.dtReportResult.Rows.Count > 0)
                    this.dtReportResult.Clear();
                if (rowCurrent != null)
                {
                    strfjy_id = rowCurrent["fjy_id"].ToString();
                    //strfsample_id = rowCurrent["fjy_id"].ToString();
                    strfinstr_result_id = rowCurrent["finstr_result_id"].ToString();
                    


                    if (rowCurrent["fjy_zt"] != null)
                        strfexamine_flag = rowCurrent["fjy_zt"].ToString();
                    WWResultList();
                    WWImg();
                }

            }
            catch (Exception ex)
            {
                WWMessage.MessageShowError(ex.ToString());
            }

            
        }


        private void dataGridViewResult_DataError(object sender, DataGridViewDataErrorEventArgs e)
        {

        }

        private void toolStripButtonHelp_Click(object sender, EventArgs e)
        {
            this.WWOpenHelp("lis_jyquery");
        }

        private void toolStripButtonPrintYL_Click(object sender, EventArgs e)
        {
            WWPrint(1);
        }

        private void toolStripButtonPrint_Click(object sender, EventArgs e)
        {
          //  MessageBox.Show(this.strfsample_id);
            WWPrint(0);
        }

        private void toolStripButtonQuery_Click(object sender, EventArgs e)
        {
            WWSetReportList();           
        }        
 
        private void dataGridViewReport_Click(object sender, EventArgs e)
        {
            try
            {
                if (dataGridViewReport.Rows.Count > 0)
                {
                    strfjy_id = this.dataGridViewReport.CurrentRow.Cells["fjy_id"].Value.ToString();
                    WWResultList();
                }
            }
            catch (Exception ex)
            {
                WWMessage.MessageShowError(ex.ToString());
            }
        }

        private void dataGridViewReport_DataError(object sender, DataGridViewDataErrorEventArgs e)
        {

        }

        private void 导入图片ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                ImgBLL img = new ImgBLL();
                img.ImgIn(img1);
            }
            catch (Exception ex)
            {
                WWMessage.MessageShowError(ex.ToString());
            }
        }

        private void 导出图片ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                ImgBLL img = new ImgBLL();
                img.ImgOut(img1);
            }
            catch (Exception ex)
            {
                WWMessage.MessageShowError(ex.ToString());
            }
        }

        private void 清空图片ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                ImgBLL img = new ImgBLL();
                img.ImgNull(img1);
            }
            catch (Exception ex)
            {
                WWMessage.MessageShowError(ex.ToString());
            }
        }

        private void 图片浏览ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                Bitmap bit = new Bitmap(img1.Image);
                ImgShowForm imgform = new ImgShowForm(bit);
                imgform.ShowDialog();
                imgform.Dispose();
            }
            catch (Exception ex)
            {
                WWMessage.MessageShowError(ex.ToString());
            }
        }

        private void img_DoubleClick(object sender, EventArgs e)
        {
            try
            {
                PictureBox pic = (PictureBox)sender;
                Bitmap bit = new Bitmap(pic.Image);
                ImgShowForm imgform = new ImgShowForm(bit);
                imgform.ShowDialog();
                imgform.Dispose();
            }
            catch (Exception ex)
            {
                WWMessage.MessageShowError(ex.ToString());
            }
        }

        private void fjy_instrComboBox_DropDownClosed(object sender, EventArgs e)
        {
            if (object.Equals(null, this.fjy_instrComboBox.SelectedValue)) return;
            string str仪器id = "";//
            {
                str仪器id = this.fjy_instrComboBox.SelectedValue.ToString();
            }
            bllResultShow(str仪器id);
           
        }

        private void dataGridViewReport_DataError_1(object sender, DataGridViewDataErrorEventArgs e)
        {

        }

        private void dataGridViewResult_DataError_1(object sender, DataGridViewDataErrorEventArgs e)
        {

        }


        private void dataGridViewReport_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (object.Equals(null, dataGridViewReport.CurrentRow)) return;

            if (this.dataGridViewReport.CurrentRow.Cells["备注"].Value.ToString() == "")
            {
                string fjy_id = this.dataGridViewReport.CurrentRow.Cells["fjy_id"].Value.ToString();
                this.dataGridViewReport.CurrentRow.Cells["备注"].Value = this.blljy.BlltOtherData(fjy_id);
            }

            Clipboard.SetDataObject(dataGridViewReport.CurrentRow.Cells["备注"].Value);
        }

        private void tsb_updateprinter_Click(object sender, EventArgs e)
        {
            Form_dysz frm = new Form_dysz();
            frm.ShowDialog();
        }

        private void tsb_allprint_Click(object sender, EventArgs e)
        {
            foreach (DataGridViewRow row in dataGridViewReport.Rows)
            {
                if (row.Cells["isSelect"].Value.ToString() == "1")
                {
                    this.strfjy_id = row.Cells["fjy_id"].Value.ToString();
                    WWPrint(0);
                }
            }
        }
    }
}